package com.weiyi.mashangzhu.Roomer.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.HouseDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Activity.RecommendActivity;
import com.weiyi.mashangzhu.Roomer.Bean.ThemeHavePageBean;

import java.util.List;
public class HomeHotAdapter extends PagerAdapter {

    private Context context;
    private List<ThemeHavePageBean.DataBean.HouselistBean> list;
    int i=0;

    public HomeHotAdapter(Context context, List<ThemeHavePageBean.DataBean.HouselistBean> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return i=list.size() <= 3 ? list.size()+1 : 3+1;
        } else {
            return 0;
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view;
        if (position==i-1){
            view = View.inflate(context, R.layout.item_more, null);
        }else {
            view = View.inflate(context, R.layout.item_home_hot, null);
            ImageView hotImg = (ImageView) view.findViewById(R.id.hot_img);
            TextView hotPrice = (TextView) view.findViewById(R.id.hot_price);
            TextView hotAddress = (TextView) view.findViewById(R.id.hot_address);
            TextView hotContent = (TextView) view.findViewById(R.id.hot_content);
            Glide.with(context).load(API.BaseApi_url+list.get(position).getHImg1()).into(hotImg);
            hotPrice.setText("¥"+list.get(position).getHPrice());
            hotAddress.setText(list.get(position).getHProvince()+" "+list.get(position).getHCity());
            hotContent.setText(list.get(position).getHIntro()+"");
            view.setTag(position);
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position<list.size()){
                    Intent intent=new Intent(context, HouseDetailsActivity.class);
                    intent.putExtra("id",list.get(position).getId()+"");
                    intent.putExtra("city",list.get(position).getHCity()+"");
                    context.startActivity(intent);
                }else {
                    Intent intent=new Intent(context, RecommendActivity.class);
                    intent.putExtra("info","hothouse");
                    context.startActivity(intent);
                }
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}